<template>
  <div class="app-container">
    <div class="filter-container">
      <!--查询表单-->
      <el-form :inline="true" class="demo-form-inline" size="mini">

        <el-form-item>
          <el-input v-model="articleQuery.title" placeholder="标题"/>
        </el-form-item>

        <el-form-item label="添加时间">
          <el-date-picker
            v-model="articleQuery.begin"
            type="datetime"
            placeholder="选择开始时间"
            value-format="yyyy-MM-dd HH:mm:ss"
            default-time="00:00:00"
          />
        </el-form-item>

        <el-form-item>
          <el-date-picker
            v-model="articleQuery.end"
            type="datetime"
            placeholder="选择截止时间"
            value-format="yyyy-MM-dd HH:mm:ss"
            default-time="00:00:00"
          />
        </el-form-item>

        <el-button type="primary" icon="el-icon-search" size="mini" @click="getList()">查询</el-button>
        <el-button type="default" icon="el-icon-refresh" size="mini" @click="resetData()">清空</el-button>
        <el-button type="primary" icon="el-icon-circle-plus-outline" size="mini" @click="goEdit()"
                   v-permission="'article:add'">添加
        </el-button>

      </el-form>
    </div>
    <el-table :data="list" v-loading="listLoading" border fit
              highlight-current-row>
      <el-table-column align="center" label="序号" width="80">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="categoryName" label="分类"></el-table-column>

      <el-table-column label="封面" width="150">
        <template slot-scope="scope">
          <img :src="scope.row.coverImage" class="cover-image" alt="封面">
        </template>
      </el-table-column>
      <el-table-column align="center" prop="title" label="标题" width="460"></el-table-column>
      <el-table-column align="center" prop="price" label="价格"></el-table-column>
      <el-table-column align="center" prop="commentsCount" label="评论数"></el-table-column>
      <el-table-column align="center" prop="viewCount" label="浏览量"></el-table-column>
      <el-table-column align="center" prop="voteCount" label="点赞"></el-table-column>

      <el-table-column align="center" prop="gmtCreate" label="创建时间" width="170"/>
      <el-table-column align="center" prop="gmtModified" label="最近修改时间" width="170"/>
      <el-table-column align="center" label="管理" width="200" fixed="right">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="goEdit(scope.row.id)"
                     v-permission="'article:update'">修改
          </el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeDataById(scope.row.id)"
                     v-permission="'article:delete'">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      background
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="getList"
    />
  </div>
</template>
<script>
  export default {
    data() {
      return {
        articleQuery: {},
        list: [],//表格的数据
        listLoading: false,//数据加载等待动画
        page: 1, //当前页
        limit: 5, //每页记录数
        total: 0, //总记录数
      }
    },
    created() {
      this.getList();
    },
    methods: {
      getList() {
        //查询列表
        if (!this.hasPerm('article:list')) {
          return
        }
        this.listLoading = true;
        this.api({
          url: `/article/pageListArticleCondition/${this.page}/${this.limit}`,
          method: "post",
          data: this.articleQuery
        }).then(data => {
          this.listLoading = false;
          this.list = data.list;
          this.total = data.totalCount;
        })
      },
      //重置查询条件
      resetData() {
        this.articleQuery = {}
        this.getList()
      },
      goEdit(id) {
        let s = id ? '/' + id : '';
        this.$router.push('/article/edit' + s)
      },
      removeDataById(id) {
        this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          return this.api({
            url: `/article/${id}`,
            method: "delete",
          })
        }).then(() => {
          this.getList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch((response) => { // 失败
          if (response === 'cancel') {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          } else {
            this.$message({
              type: 'error',
              message: '删除失败'
            })
          }
        })
      }

    }
  }
</script>
<style scoped>


  .cover-image {
    width: 120px;
    aspect-ratio: auto 120 / 90;
    height: 90px;
  }
</style>
